package br.com.j7.jdesk.persistence.repository.impl;

import java.util.List;

import br.com.j7.jdesk.library.exception.RepositoryException;
import br.com.j7.jdesk.library.util.CriptUtils;
import br.com.j7.jdesk.persistence.entity.Enterprises;
import br.com.j7.jdesk.persistence.entity.Users;
import br.com.j7.jdesk.persistence.repository.UserRepository;

/**
 * 
 * 
 * @author Edivando Alves
 *
 */
public class UserRepositoryImpl  extends AbstractRepository<Users> implements UserRepository{
	

	/*
	 * (non-Javadoc)
	 * @see br.com.j7.jdesk.persistence.repository.impl.AbstractRepository#add(java.lang.Object)
	 */
	@Override
	public Users add(Users entity) throws RepositoryException {
		entity.setCdPassword(CriptUtils.md5(entity.getCdPassword()));  
		entity.setCdConfirmPassword(CriptUtils.md5(entity.getCdConfirmPassword()));
		try {
			return super.add(entity);
		} catch (RepositoryException e) {
			throw new RepositoryException(e);
		}
	}
	
	/*
	 * (non-Javadoc)
	 * @see br.com.j7.jdesk.persistence.repository.impl.AbstractRepository#find(java.lang.Integer)
	 */
	@Override
	public Users find(Integer id) throws RepositoryException{
		try {
			return getEntity(Users.class, id);
		} catch (RepositoryException e) {
			throw new RepositoryException(e);
		}
	}
	
	/*
	 * (non-Javadoc)
	 * @see br.com.j7.jdesk.persistence.repository.impl.AbstractRepository#list()
	 */
	@Override
	public List<Users> list() throws RepositoryException{
		try {
			return listByNamedQuery(Users.class,"user");
		} catch (RepositoryException e) {
			throw new RepositoryException(e);
		}
	}
	
	/*
	 * (non-Javadoc)
	 * @see br.com.j7.jdesk.persistence.repository.UserRepository#listByEnterprise(br.com.j7.jdesk.persistence.entity.Enterprises)
	 */
	@Override
	public List<Users> listByEnterprise(Enterprises enterprise) throws RepositoryException{
		try {
			return listByNamedQuery(Users.class, "userByEnterprise", enterprise);
		} catch (RepositoryException e) {
			throw new RepositoryException(e);
		}
	}
	
	/*
	 * (non-Javadoc)
	 * @see br.com.j7.jdesk.persistence.repository.UserRepository#listByEnterpriseAndLogin(java.lang.String, java.lang.String)
	 */
	@Override
	public Users listByEnterpriseAndLogin(String enterprise, String cdLogin) throws RepositoryException {
		try {
			return getEntity(Users.class,  "userByEnterpriseAndCdLogin", enterprise, cdLogin);
		} catch (RepositoryException e) {
			throw new RepositoryException(e);
		}
	}
}
